package com.itheima.sc.service.sms;

import cn.hutool.core.convert.Convert;
import com.itheima.sc.api.sms.SmsFlashProductRelationApi;
import com.itheima.sc.api.sms.SmsFlashPromotionSessionApi;
import com.itheima.sc.config.ComException;
import com.itheima.sc.pojo.sms.FlashPromotionProductRelation;
import com.itheima.sc.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SmsFlashProductRelationService {

    @DubboReference
    private SmsFlashProductRelationApi relationApi;
    @DubboReference
    private SmsFlashPromotionSessionApi sessionApi;



    //批量添加秒杀活动的关系
    public void addRelation(List<FlashPromotionProductRelation> relations) {
        relationApi.addRelations(relations);
    }

    //删除商品与秒杀活动的关系  //删除需要三思后行
    public void deleteRelation(Long id) {
        FlashPromotionProductRelation relation = relationApi.selectRelation(id);
        //首先查看session是否存在
        boolean flag = sessionApi.existSession(relation.getFlashPromotionSessionId());
        if (flag){
            relationApi.deleteRelation(id);
        }
            throw new ComException("数据不存在");
    }
    //分页查询不同场次关联及商品信息
    public PageResult productRelationList(Map<String, String> param) {
        //取出数据
        int page = Convert.toInt(param.get("pageNum"));
        int pageSize = Convert.toInt(param.get("pageSize"));
        int flashPromotionId = Convert.toInt(param.get("flashPromotionId"));//秒杀活动的id
        int sessionId = Convert.toInt(param.get("flashPromotionSessionId"));//场次id
        return relationApi.selectRelationBySession(page, pageSize, flashPromotionId, sessionId);

    }
    //修改关联信息
    public void updateProductRelation(Long id, FlashPromotionProductRelation relation) {
        //这个id是商品关联表中的id字段
        relationApi.updateProductRelation(id,relation);
    }

    //获取管理商品促销信息
    public FlashPromotionProductRelation getProductRelation(Long id) {
        return relationApi.selectRelation(id);
    }
    //获取相关时段进行秒杀的产品种类
    public int findRelation(Long sessionId) {
        return relationApi.findRelationCount(sessionId);
    }
}
